﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace hyjiacan.util.web.wp
{
    public partial class OptionForm : hyjiacan.util.winform.JForm
    {
        public OptionForm(string title)
        {
            InitializeComponent();
            CheckForIllegalCrossThreadCalls = false;
        }

        private void OptionForm_Load(object sender, EventArgs e)
        {
            backgroundWorker.RunWorkerAsync("load");
        }

        private void backgroundWorker_DoWork(object sender, DoWorkEventArgs e)
        {
            string arg = e.Argument.ToString();
            if (arg == "load")
            {
                SetStatus("正在加载数据...");
                try
                {
                    DataTable data = DBUtil.Select("select option_name, option_value from {prefix}options where option_name in ('home', 'blogname', 'blogdescription')");
                    // home 是前台地址
                    foreach (DataRow row in data.Rows)
                    {
                        string name = DBUtil.GetCellValue(row, 0);
                        string value = DBUtil.GetCellValue(row, 1);
                        Invoke((MethodInvoker)delegate()
                        {

                            if (name == "home")
                            {
                                tHome.Text = value;
                                try
                                {
                                    // 根据地址加载logo
                                    this.SetIcon(value + "/favicon.ico");
                                }
                                catch (Exception ex)
                                {
                                    SetStatus("图标加载失败", Color.Red);
                                }
                            }
                            else if (name == "blogname")
                            {
                                title.Text = tName.Text = value;
                            }
                            else if (name == "blogdescription")
                            {
                                tSubTitle.Text = value;
                            }

                        });
                    }
                    SetStatus("加载数据完成", Color.Green);
                }
                catch (Exception ex)
                {
                    SetStatus("加载数据失败", Color.Red);
                    MessageBox.Show(this, ex.Message, "加载失败");
                }
            }
        }



        private void SetStatus(string msg)
        {
            SetStatus(msg, Color.Black);
        }

        private void SetStatus(string msg, Color color)
        {
            this.status.ForeColor = color;
            this.status.Text = msg;
        }
    }
}
